<template>
  <div class="wrap" v-if="hasInfo">
    <div class="main">
      <div class="cover">
        <img class="img" v-lazy="mediaInfo.cover + '@300w_400h'" />
      </div>
      <div class="content">
        <div class="title">{{ mediaInfo.title }}</div>
        <div class="count">
          <span class="view"><i class="fa fa-bofangshu"></i> {{ stat.views | formatNumber }}</span>
          <span class="fav"><i class="fa fa-heart"></i> {{ stat.favorites | formatNumber }}</span>
        </div>
        <div class="state">{{ info.newestEp.desc }}</div>
        <div class="desc" :class="{expand: isExpand}" @click="toggleExpand">
          <span class="btn"><i class="fa" :class="[isExpand ? 'fa-dropup':'fa-dropdown']"></i></span>
          {{ mediaInfo.evaluate }}
        </div>
        <div class="score">
          <span class="rating"><em>{{ score }}</em></span>
          <span class="num">{{ reviewCount }}</span>
        </div>
      </div>
    </div>
    <div class="toolbar">
      <div class="action">
        <van-tag class="btn fav" :class="{'btn-active': favChecked}" size="large" @click="setFav">
          <van-icon name="star" v-if="!favChecked"/>{{ favText }}
        </van-tag>
        <van-tag class="btn" size="large" @click="openShare">
          <van-icon name="share"/>分享
        </van-tag>
      </div>
      <share-sheet
        ref="share"
        :title="mediaInfo.title"
        :pic="mediaInfo.cover"
        :url="shareUrl"
      />
    </div>
  </div>
</template>

<script>
import { Icon as VanIcon, Tag as VanTag } from 'vant'
import ShareSheet from '@/components/share-sheet/ShareSheet.vue'
import { formatNumber } from '@/filters/filters.js'
export default {
  name: 'BangumiInfo',
  components: {
    VanIcon,
    VanTag,
    ShareSheet
  },
  props: {
    info: {
      type: Object
    },
    favChecked: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isExpand: false,
      shareUrl: location.href
    }
  },
  computed: {
    hasInfo () {
      return this.info !== null
    },
    favText () {
      return this.favChecked ? '已追番' : '追番'
    },
    mediaInfo () {
      return this.hasInfo ? this.info.mediaInfo : {}
    },
    stat () {
      return this.hasInfo ? this.info.seasonStat : {}
    },
    pubInfo () {
      return this.hasInfo ? this.info.pubInfo : {}
    },
    rating () {
      return this.hasInfo ? this.info.mediaRating : {}
    },
    score () {
      return this.rating.score ? `${this.rating.score}分` : '暂无评分'
    },
    reviewCount () {
      return this.rating.count ? `${this.rating.count}人` : (this.pubInfo.is_started === 0 ? '未开播' : '评分人数不足')
    }
  },
  filters: {
    formatNumber
  },
  methods: {
    toggleExpand () {
      this.isExpand = !this.isExpand
    },
    openShare () {
      this.$refs.share.show()
    },
    setFav () {
      this.$emit('fav-click')
    }
  }
}
</script>

<style lang="scss" scoped>
  .wrap{
    display: flex;
    flex-direction: column;
    padding: 10px;
    @include border1px (#e7e7e7, 0 0 1px 0)
  }
  .main {
    display: flex;
  }
  .cover{
    @include cover(75, 100, 6px);
    width: 75px;
    height: 100px;
    margin-right: 10px;
  }
  .content{
    position: relative;
    flex: 1;
    color: $color-text-secondary;
    font-size: 12px;
  }
  .title{
    width: 200px;
    height: 20px;
    font-size: 16px;
    color: $color-text-primary;
    @include max-lines(1);
  }
  .count {
    margin-bottom: 5px;
    .fa {
      font-size: 12px;
    }
    .view{
      margin-right: 10px;
    }
  }
  .state{
    margin-bottom: 5px;
  }
  .desc{
    position: relative;
    padding-right: 16px;
    line-height: 16px;
    @include max-lines(3);
    .btn {
      position: absolute;
      right: 0;
      top: 0;
      .fa{
        font-size: 16px;
      }
    }
    &.expand{
      display: block;
    }
  }
  .score{
    position: absolute;
    right: 0;
    top: 0;
    width: 70px;
    display: flex;
    align-items: center;
    flex-direction: column;
    font-size: 12px;
    color: $color-text-secondary;
    white-space: nowrap;
  }
  .rating{
    color: #ffa726;
    margin-bottom: 7px;
    em{
      margin-right: 3px;
      font-size: 18px;
      font-style: normal;
      white-space: nowrap;
    }
  }
  .toolbar{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 10px;
    color: $color-text-secondary;
    font-size: 12px;
    .action{
      display: flex;
      align-items: center;
      .btn {
        margin-right: 5px;
        color: $color-text-secondary;
        background-color: #fff;
        font-size: 14px;
        &-active{
          color: $color-white;
          background-color: $theme-color;
        }
      }
    }
    .fa {
      margin-right: 5px;
      font-size: 12px;
    }
  }
</style>
